import 'package:flutter/material.dart';
import 'package:macnov/utils/colorUtil.dart';

typedef SearchChange = void Function(String text);


class SearchBox extends StatefulWidget {
  final SearchChange onChange;

  const SearchBox({Key key, this.onChange}) : super(key: key);

  @override
  _SearchBoxState createState() => _SearchBoxState(onChange);
}

class _SearchBoxState extends State<SearchBox> {
  final SearchChange onChange;
  TextEditingController controller = TextEditingController();

  _SearchBoxState(this.onChange);

  Widget buildTextField() {
    controller.addListener(() {
      print('input ${controller.text}');
      if (onChange != null) {
        onChange(controller.text);
      }
    });
    return TextField(
      controller: controller,
      cursorColor: Colors.black,
      decoration: InputDecoration(
          contentPadding: new EdgeInsets.only(top: 10.0),
          border: InputBorder.none,
          prefixIcon: Icon(
            Icons.search,
            color: ColorsUtil.hexColor(0xBCBCBC),
          ),
          hintText: "search..",
          hintStyle: new TextStyle(
              fontSize: 16, color: ColorsUtil.hexColor(0xBCBCBC))),
      style: new TextStyle(
        fontSize: 16,
        color: Colors.black87,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget editView() {
      return Container(
        height: 45,
        padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
        decoration: new BoxDecoration(
          border: Border.all(
              color: Color.fromRGBO(227, 227, 227, 0.4), width: 1.0), //灰色的一层边框
          color: Colors.white,
          borderRadius: new BorderRadius.all(new Radius.circular(25.0)),
        ),
        alignment: Alignment.center,
        child: buildTextField(),
      );
    }

    return editView();
  }
}
